﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Triangles
{
    class Program
    {
        const int listCount = 3;
        const string Beginning = "Enter {0} triangles.\nType \"{1}\" for quit.";
        const string Exit = "End";
        const string Error = "Again!!!";
        const string listStr = "============= Triangle list: ===============";

        public static void ImportListFormConsole(ref List<Triangle> list)
        {
            string input = string.Empty;
            int i = 0;
            double d = 0;
            list.Add(null);
            while (i < listCount && input != Exit)
            {
                Console.WriteLine("Triangle {0}:", i + 1);
                Console.WriteLine("Triangle Name:");
                input = Console.ReadLine();
                list[i] = new Triangle(input);
                for (int j = 0; j < Triangle.SidesCount; j++)
                {
                    Console.WriteLine("Side {0}:", j);
                    input = Console.ReadLine();
                    while (!double.TryParse(input, out d) && input != Exit)
                    {
                        if (input != string.Empty) Console.WriteLine(Error);
                        input = Console.ReadLine();
                    }
                    list[i][j] = d;
                }
                if (!list[i].IsCorrect)
                {
                    Console.WriteLine(Error);
                }
                else
                {
                    i++;
                    if (i < listCount)
                    {
                        list.Add(null);
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            var list = new List<Triangle>(listCount);
            Console.WriteLine(Beginning, listCount, Exit);
            ImportListFormConsole(ref list);
            list.Sort();
            Console.WriteLine(listStr);
            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadLine();
        }
    }
}
